package com.glory.gloryquickui.view

import android.content.Context
import android.content.Intent
import android.media.Image
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.glory.gloryquickui.R
import com.glory.gloryquickui.activity.DemoActivity
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.utils.FreeApi_ToastUtils
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.edit.ClearEditText
import com.glory.hiwork.mvvm.bean.ToastMessageBean
import org.json.JSONObject


/**
 *单行输入框
 */
class SingleEditTextController(context: Context?,val viewModel:QuickUIViewModel) : ViewController<FieldBean?>(context) {
    private var tvSingleEditName: TextView? = null
    private var tvShowLimit: TextView? = null
    private var tvSingleEditValue: ClearEditText? = null
    private var tvSingleRightImage:ImageView? = null
    override fun resLayoutId(): Int {
        return R.layout.layout_single_edittext
    }

    override fun onBindView(data: FieldBean?) {
        tvSingleEditName!!.text = data?.config?.label
        tvSingleEditValue!!.hint = data?.placeholder
        if (data!!.config.defaultValue != null && data.config.defaultValue is String) {
            tvSingleEditValue!!.setText(data.config.defaultValue.toString())
        }
        //设置可禁止
        tvSingleEditValue!!.isEnabled = !data.disabled
        if (data.readonly) {
            tvSingleEditValue!!.inputType = InputType.TYPE_NULL
        }

        //是否显示右箭头
        if(data.suffixIcon == "1"){
            tvSingleRightImage?.visibility = View.VISIBLE
        }


        //设置最大字符
//        tvSingleEditValue?.setMaxLength(data.maxlength.toString())
        //设置前后图标
        tvSingleEditValue?.setLeftAndRightIcon(data.prefixIcon, data.suffixIcon)

        //设置字符统计
        tvShowLimit?.visibility = if (data.showWordLimit) View.VISIBLE else View.GONE
        tvShowLimit?.text = "0/" + data.maxlength.toString()
        tvSingleEditValue?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                tvShowLimit?.text =
                    tvSingleEditValue?.text.toString().length.toString() + "/" + data.maxlength.toString()
                if (!data.readonly) {
                    val map = viewModel.pageData.value!!.toMutableMap()
                    map[data.vModel] = p0.toString()
                    viewModel.pageData.value = map
                    if (p1 == 0 && p2 == 0 && p3 > 1) {
                        if (p0.toString().isNotEmpty() && data.slot.append == "1") {
                            viewModel.searchData()
                        }
                    }
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })

        if(data.config.eventType == "customEvent" && data.config.eventContent.isNotEmpty()){
            try {
                val json = JSONObject(data.config.eventContent)
                if(json.getString("ActionType") == "OnClick"){

                    view!!.setOnClickListener {

                        if(json.getString("ActionEvent") == "jump"){
                            val intent = Intent(context, DemoActivity::class.java)
                            intent.putExtra("json", json.getString("parameter"))
                            context!!.startActivity(intent)
                        }

                    }

                }
            } finally {
                viewModel.defUI.toastEvent.value = ToastMessageBean("解析异常",false)
            }
        }
    }


    override fun onCreatedView(view: View?) {
        tvSingleEditName = view?.findViewById(R.id.tvSingleEditName)
        tvSingleEditValue = view?.findViewById(R.id.tvSingleEditValue)
        tvShowLimit = view?.findViewById(R.id.tvShowLimit)
        tvSingleRightImage = view?.findViewById(R.id.tvSingleRightImage)
    }

    override fun masterPart(): View? {
        return tvSingleEditValue
    }

}
